//
//  XYButton.m
//  XYAuto
//
//  Created by 小萌 on 2016/12/22.
//  Copyright © 2016年 Raymond~. All rights reserved.
//

#import "XYButton.h"

@implementation XYButton

- (instancetype)init
{
    if (self = [super init]) {
        [self initValue];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initValue];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self initValue];
    }
    return self;
}

- (void)initValue
{
    self.titlePosition = XYButtonTitlePositionNomal;
    self.spacing = 5.f;
    self.fontFloat = 14.f;
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
}
- (void)setFontFloat:(CGFloat)fontFloat
{
    if (_fontFloat != fontFloat) {
        _fontFloat = fontFloat;
    }
    self.titleLabel.font = [UIFont systemFontOfSize:_fontFloat];
}
- (void)setTitlePosition:(XYButtonTitlePosition)titlePosition
{
    if (_titlePosition != titlePosition) {
        _titlePosition = titlePosition;
    }
    if (_titlePosition == XYButtonTitlePositionNomal) {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }else if (_titlePosition == XYButtonTitlePositionLeft){
        self.titleLabel.textAlignment = NSTextAlignmentRight;
    }else{
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    switch (self.titlePosition) {
        case XYButtonTitlePositionTop:
        {
            CGFloat titleY = contentRect.size.height * 0.5 - self.spacing * 0.5 - self.fontFloat;
            return CGRectMake(0, titleY, contentRect.size.width , self.fontFloat);
        }
            break;
        case XYButtonTitlePositionLeft:
        {
            CGSize textSize = [NSString heightForString:self.titleLabel.text Size:CGSizeMake(contentRect.size.width - self.spacing - self.imageView.width, contentRect.size.height) Font:[UIFont systemFontOfSize:self.fontFloat]].size;
            CGFloat width = contentRect.size.width - self.imageView.width - self.spacing;
            if (width > textSize.width) {
                width = textSize.width;
            }
            return CGRectMake((contentRect.size.width - width - self.spacing - self.imageView.width) * 0.5, 0,width, contentRect.size.height);
        }
            break;
        case XYButtonTitlePositionBottom:
        {
//            CGFloat titleY = contentRect.size.height  - self.spacing - self.fontFloat;

            return CGRectMake(-10,self.imageView.originY + self.imageView.height + self.spacing, contentRect.size.width + 20, self.fontFloat);
        }
            break;
        case XYButtonTitlePositionNomal:
        {
//            return CGRectMake(contentRect.size.width * 0.5 + self.spacing * 0.5, 0, contentRect.size.width * 0.5 - self.spacing * 0.5, contentRect.size.height);
            return [super titleRectForContentRect:contentRect];
        }
            break;
        default:
            return [super titleRectForContentRect:contentRect];
            break;
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    switch (self.titlePosition) {
        case XYButtonTitlePositionTop:
        {
            CGFloat imageY = contentRect.size.height * 0.5 + self.spacing * 0.5;
            CGFloat imageBottom = self.spacing;
            CGFloat imageWidth,imageHeight;
            if (self.imageSize.width != 0 && self.imageSize.height != 0) {
                imageWidth = self.imageSize.width;
                imageHeight = self.imageSize.height;
            }else{
                imageHeight = contentRect.size.height - imageY - imageBottom;
                UIImage *image =  [self imageForState:self.state];
                imageWidth = image.size.width *(imageHeight) / image.size.height;
            }
            return CGRectMake((contentRect.size.width - imageWidth) * 0.5, imageY, imageWidth, imageHeight);
        }
            break;
        case XYButtonTitlePositionLeft:
        {
            CGFloat imageY,imageWidth,imageHeight;
            if (self.imageSize.width != 0 && self.imageSize.height != 0) {
                imageWidth = self.imageSize.width;
                imageHeight = self.imageSize.height;
                imageY = (contentRect.size.height - imageHeight) * 0.5;
            }else{
                UIImage *image =  [self imageForState:self.state];
                imageWidth = image.size.width;
                imageHeight = image.size.height;
                imageY = (contentRect.size.height - imageHeight) * 0.5;
            }
            CGSize textSize = [NSString heightForString:self.titleLabel.text Size:CGSizeMake((contentRect.size.width - imageWidth - self.spacing), contentRect.size.height) Font:[UIFont systemFontOfSize:self.fontFloat]].size;

            CGFloat imageX = (contentRect.size.width - textSize.width - imageWidth - self.spacing) * 0.5 + textSize.width + self.spacing;
            return CGRectMake(imageX, imageY, imageWidth, imageWidth);
        }
            break;
        case XYButtonTitlePositionBottom:
        {
            CGFloat imageX,imageY,imageWidth,imageHeight;
            if (self.imageSize.width != 0 && self.imageSize.height != 0) {
                imageWidth = self.imageSize.width;
                imageHeight = self.imageSize.height;
            }else{
                UIImage *image =  [self imageForState:self.state];
                imageWidth = image.size.width;
                imageHeight = image.size.height;
            }
            
            imageY = (contentRect.size.height - self.fontFloat - imageHeight - self.spacing)/2.0;
            
            imageX = (contentRect.size.width - imageWidth) * 0.5;
            return CGRectMake(imageX, imageY, imageWidth, imageHeight);
        }
            break;
        case XYButtonTitlePositionNomal:
        {
            CGFloat imageX,imageY,imageWidth,imageHeight;
            if (self.imageSize.width != 0 && self.imageSize.height != 0) {
                imageWidth = self.imageSize.width;
                imageHeight = self.imageSize.height;
                imageY = (contentRect.size.height - imageHeight) * 0.5;
            }else{
                UIImage *image =  [self imageForState:self.state];
                imageWidth = image.size.width;
                imageHeight = image.size.height;
                imageY = (contentRect.size.height - imageHeight) * 0.5;
            }
            imageX = contentRect.size.width * 0.5 - self.spacing *0.5 - imageWidth;
            return CGRectMake(imageX, imageY, imageWidth, imageWidth);
        }

            return [super imageRectForContentRect:contentRect];
            break;
        default:
            return [super imageRectForContentRect:contentRect];
            break;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
